package com.sdgakj.service.feed;

import com.google.common.collect.Maps;
import com.sdgakj.dto.FeedTagDTO;
import com.sdgakj.entity.FeedTag;
import com.sdgakj.enums.EnumReviewType;
import com.sdgakj.repository.FeedTagDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.mapper.BeanMapper;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class FeedTagService {

	@Autowired
	private FeedTagDao feedTagDao;
	
	public FeedTag getFeedTagById(Long id) {
		return feedTagDao.findOne(id);
	}
	
	public List<FeedTag> getFeedTagList() {
		return (List<FeedTag>) feedTagDao.findAll();
	}
	
	public List<FeedTagDTO> getAll() {
		List<FeedTagDTO> result = new ArrayList<FeedTagDTO>();
		List<FeedTag> tags = feedTagDao.findAllByOrderBySeqAsc();
		if(tags != null && !tags.isEmpty()) {
			result = BeanMapper.mapList(tags, FeedTagDTO.class);
		}
		return result;
		
	}
	
	public List<FeedTag> getFeedTagList(String sortName,String sortType) {
		Sort sort = buildSortRequest(sortName,sortType);
		return (List<FeedTag>) feedTagDao.findAll(sort);
	}
	
	public void saveFeedTag(FeedTag entity) {
		feedTagDao.save(entity);
//		new Runnable() {
//			@Override
//			public void run() {
//				initIndexCondition.initCondition("orderno", "desc");
//			}
//		}.run();
	}
	
	public void updateFeedTag(FeedTag entity) {
		feedTagDao.save(entity);
//		new Runnable() {
//			@Override
//			public void run() {
//				initIndexCondition.initCondition("orderno", "desc");
//			}
//		}.run();
	}
	
	
	public List<FeedTag> getFeedTagByName(String name) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (StringUtils.isNotBlank(name)) {
			filters.put("name", new SearchFilter("name", Operator.EQ, name));
		}
		Specification<FeedTag> spec = DynamicSpecifications.bySearchFilter(filters.values(), FeedTag.class);
		return feedTagDao.findAll(spec);
	}
	
	public void deleteFeedTag(Long id) {
		FeedTag feedTag = this.getFeedTagById(id);
		feedTag.setState(EnumReviewType.UNUSED.getCode());
		this.saveFeedTag(feedTag);
	}
	
	public List<FeedTag> getFeedTagAllByState(Integer deltag) {
		return (List<FeedTag>) feedTagDao.findTagByState(deltag);
	}
	
	public List<FeedTag> getPriceTypeAllByTag(String deltag,String sortName,String sortType) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if (StringUtils.isNotBlank(deltag)) {
			filters.put("deltag", new SearchFilter("deltag", Operator.EQ, deltag));
		}
		Specification<FeedTag> spec = DynamicSpecifications.bySearchFilter(filters.values(), FeedTag.class);
		Sort sort = buildSortRequest(sortName,sortType);
		return feedTagDao.findAll(spec, sort);
	}

	public Page<FeedTag> getFeedTagByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName,
			String sortType) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortName, sortType);
		Specification<FeedTag> spec = buildSpecification(searchParams);
		return feedTagDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建排序.
	 */
	private Sort buildSortRequest(String sortName,String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}else if("desc".equals(sortType)){
			sort = new Sort(Direction.DESC, sortName);
		}else{
			sort = new Sort(Direction.ASC, sortName);
		}
		return sort;
	}
	
	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize,  String sortName,String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}else if("desc".equals(sortType)){
			sort =new Sort(Direction.ASC,"seq");
		}
		return new PageRequest(pageNumber - 1, pagzSize, sort);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<FeedTag> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		Specification<FeedTag> spec = DynamicSpecifications.bySearchFilter(filters.values(), FeedTag.class);
		return spec;
	}
	
}
